from jili.data.db import getdb_client,insert_one
from jili.tool.pcinfo import get_mac,getlocal_ip
import datetime,time
from jili.tool.convert import str2datetime
def mark_flag(name,flag=True,date=None,islocal=True,ip="127.0.0.1",port=None,dbname="stock",auth="ant:ant"):
    db=getdb_client(ip=ip,dbname=dbname,auth=auth,port=port)
    if islocal:
        name=name+"_"+getlocal_ip()+"_"+get_mac()
    now = datetime.datetime.now()
    if date is None:
        date=datetime.datetime(now.year,now.month,now.day)
    else:
        date=str2datetime(date)
    cu=db["data_update_status"]
    t={"name":name,"date":date,"flag":flag,"update":now}
    insert_one(cu,t)
    db.client.close()
def get_mark_flag(name,date=None,islocal=True,ip="127.0.0.1",port=None,dbname="stock",auth="ant:ant"):
    db=getdb_client(ip=ip,dbname=dbname,auth=auth,port=port)
    if islocal:
        name=name+"_"+getlocal_ip()+"_"+get_mac()
    if date   is None:
        now = datetime.datetime.now()
        date = datetime.datetime(now.year, now.month, now.day)
    else:
        date=str2datetime(date)
    cu = db["data_update_status"]
    t = {"name": name, "date": date}
    rst=cu.find_one(t)
    db.client.close()
    if rst:
        return rst["flag"]
    else:
        return False
def hold_until_flag(name,date=None,flag=True,times=60*24,redotimes=0.5,islocal=True,ip="127.0.0.1",port=None,dbname="stock",auth="ant:ant"):
    times=times*60
    rst=False
    redotimes=redotimes*60
    # if islocal:
    #     name=name+"_"+getlocal_ip()+"_"+get_mac()
    while times>0:
        r=get_mark_flag(name,date,islocal=islocal,ip=ip,dbname=dbname,auth=auth)
        if flag==r:
            rst=True
            break
        times=times-redotimes
        time.sleep(redotimes)
    return rst
def hold_until_flags(name,date=None,flag=True,times=60*24,redotimes=0.5,islocal=True,ip="127.0.0.1",port=None,dbname="stock",auth="ant:ant"):
    times=times*60
    rst=False
    redotimes=redotimes*60
    # if islocal:
    #     name=name+"_"+getlocal_ip()+"_"+get_mac()
    while times>0:
        if isinstance(name,str):
            r=get_mark_flag(name,date,islocal=islocal,ip=ip,dbname=dbname,auth=auth)
            if flag==r:
                rst=True
                break
        else:
            n0=[]
            for i in name:
                r = get_mark_flag(i, date, islocal=islocal, ip=ip, dbname=dbname, auth=auth)
                if flag == r:
                    n0.append(i)
            for i in n0:
                name.remove(i)
            if not name:
                rst = True
                break
        times=times-redotimes
        time.sleep(redotimes)
    return rst
def hold_until_flags_or(name,date=None,flag=True,times=60*24,redotimes=0.5,islocal=True,ip="127.0.0.1",port=None,dbname="stock",auth="ant:ant"):
    times=times*60
    rst=False
    redotimes=redotimes*60
    # if islocal:
    #     name=name+"_"+getlocal_ip()+"_"+get_mac()
    a=[]
    while times>0:
        for i in name:
            r=get_mark_flag(i,date,islocal=islocal,ip=ip,dbname=dbname,auth=auth)
            if flag==r:
                a.append(i)
        if a:
            break
        times=times-redotimes
        time.sleep(redotimes)
    if a:
        rst=a[0]
    return rst

if __name__=="__main__":
    # hold_until_flags(name=["usa_k1d_bfq_data", "stock_basic_data"], date=datetime.datetime(2022, 12, 22),islocal=False)
    hold_until_flags_or(name=["usa_k1d_bfq_data1", "stock_basic_data"], date=datetime.datetime(2022, 12, 22), islocal=False)